package cn.wolfcode.service.impl;

import cn.wolfcode.domain.Employee;
import cn.wolfcode.domain.Permission;
import cn.wolfcode.mapper.PermissionMapper;
import cn.wolfcode.mapper.RoleMapper;
import cn.wolfcode.qo.PageResult;
import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.service.IPermissionService;
import cn.wolfcode.util.RequiredPermission;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Service;

import java.lang.reflect.Method;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Map;

@Service
public class PermissionServiceImpl implements IPermissionService {

    @Autowired
    private PermissionMapper permissionMapper;

    @Override
    public void save(Permission permission) {
        permissionMapper.insert(permission);
    }

    @Override
    public void delete(Long id) {
        permissionMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void update(Permission permission) {
        permissionMapper.updateByPrimaryKey(permission);
    }

    @Override
    public Permission get(Long id) {
        return permissionMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Permission> listAll() {
        return permissionMapper.selectAll();
    }

    //分页查询
    @Override
    public PageInfo<Permission> query(QueryObject qo) {
        //使用分页插件,传入当前页,每页显示数量
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize());
        List<Permission> permission = permissionMapper.selectForList(qo);
        return new PageInfo( permission);
    }

    @Autowired
    private ApplicationContext ctx;//spring 上下文
    @Override
    public void reload() {

        //获取数据库中的所有权限
        List<String> permissions = permissionMapper.selectAllExpression();
        //获取所有controller
        Map<String,Object>  map = ctx.getBeansWithAnnotation(Controller.class);
        Collection<Object> collections = map.values();
        //遍历所有ontroller获取里面的每个方法
        for (Object controller :collections){
            Class clzz = controller.getClass();//controller字节码对象
            Method[] methods = clzz.getDeclaredMethods();//当前类中的方法
            //遍历每个方法 判断该方法是否自定义注解
            for (Method method:methods){
                RequiredPermission annotation = method.getAnnotation(RequiredPermission.class);
                //有的话 进行下一步  没有直接跳过
                if (annotation!=null) {
                    //获取注解的名称和表达式
                    String expression = annotation.expression();
                    //判断数据库中是否有权限
                    if (!permissions.contains(expression)) {
                        String name = annotation.name();
                        //封装成权限对象
                        Permission permission = new Permission();
                        permission.setName(name);
                        permission.setExpression(expression);
                        //把对象插入到数据库
                        permissionMapper.insert(permission);
                    }
                }
            }
        }
    }

    @Override
    public List<String> selectExpressionByEmpId(Long employeeId) {

        return permissionMapper.selectExpressionByEmpId(employeeId);
    }
}
